#ifndef PROFILE_H
#define PROFILE_H

#include "Helper.h"
#include "esp_system.h"
#include "cJSON.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <stdbool.h>
#include <string.h>
#define MAX_CHANNEL_COUNT 2

// 基本配置
typedef struct {
	int network_frame_time;
	char config_password[64];
	bool dtu_log_output;
	bool dtu_firmware_update;
	bool group_param_update;
	int auto_reboot_time;
	int serial_inactive_reboot;
	int network_inactive_reboot;
	bool remote_control;
	int ntp_sync_interval;
	bool new_conn_clear_cache;
	bool low_power_mode;
	int led_indicator;
	bool enable_ipv6;
} basic_parameters_t;

// 串口配置
typedef struct {
	bool enable;
	int baud_rate;
	int data_bits;
	int parity;
	int stop_bits;
	int pack_timeout;
} serial_port_parameters_t;

// 自动轮询配置
typedef struct {
	bool enable;
	int response_timeout;
	int polling_interval;
	char command_format[32];
	char command[128];
	unsigned char command_byte[16];
} auto_polling_t;

// 网络通道配置
typedef struct {
	bool enable;
	// 通道地址
	char server_address[256];
	// 端口
	int server_port;
	// 绑定串口
	int bound_serial;
	// 通道类型：  NONE(-1),
    // TCP(0),
    // UDP(1),
    // MQTT(2),
    // HTTP(3),
    // WEB_SOCKET(4),
    // IOT_PLATFORM_TELECOM_MQTT(5),
    // IOT_PLATFORM_HUAWEI(6),
    // IOT_PLATFORM_TENCENT(7),
    // IOT_PLATFORM_ALI(8),
    // IOT_PLATFORM_TUYA(9),
    // IOT_PLATFORM_THINGSCLOUD(10);
	int type;
} network_channel_t;

typedef struct {
	int heartbeat_interval;
	char client_id[64];
	char username[32];
	char password[32];
	int protocol_version;
	bool persistent_session;
	bool persistent_message;
	int subscribe_qos;
	int publish_qos;
	char subscribe_topic[128];
	char publish_topic[128];

	bool will_enabled;
	int will_qos;
	bool will_retain;
	char will_topic[128];
	char will_message[256];
	int login_info_type;
	char login_info_content[64];
	bool ipv6_support;
	int ssl_mode;

} network_channel_mqtt_t;

// 网络通道参数配置
typedef struct {
	bool enable;
	char send_template[256];
	char receive_template[256];
} data_template_t;

// 数据流模板配置
typedef struct {
	char script[256];
} lua_script_t;

// 任务配置
typedef struct {
	bool enable;
	lua_script_t lua_scripts[8];
} task_t;

// 加密证书配置
typedef struct {
	bool enable;
	char encryption_type[32];
	char verify_mode[32];
	char server_cert[128];
	char client_cert[128];
	char client_key[128];
	char key_password[64];
} encryption_certificate_t;

// 固件升级配置
typedef struct {
	bool enable;
	char file_version[64];
	char source_type[32];
	char download_path[128];
	char bound_serial[64];
	int packet_length;
	int packet_interval;
	bool data_identifier;
} firmware_upgrade_t;

typedef struct {
	basic_parameters_t basic_parameters;
	serial_port_parameters_t serial_port_parameters[1];
	auto_polling_t auto_polling;
	network_channel_t network_channels[2];
	network_channel_mqtt_t network_channel_mqtts[2];
	data_template_t data_templates[2];
	task_t task;
	encryption_certificate_t encryption_certificates[1];
	firmware_upgrade_t firmware_upgrade;
} dtu_config_t;

#ifdef __cplusplus
extern "C" {
#endif
void spiffs_init(void);
void write_profile(const char *buffer);

dtu_config_t *read_profile(void);
#ifdef __cplusplus
}

#endif // REMOTE_CONFIG_H
#endif